package com.techfansy.provinceCitySelect;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.techfansy.provinceCitySelect.adapter.NumericWheelAdapter;
import com.techfansy.provinceCitySelect.listener.OnItemSelectedListener;
import com.techfansy.provinceCitySelect.utils.Field;

import java.util.Arrays;
import java.util.List;

public class ProvinceCitySelect {
    private static com.techfansy.provinceCitySelect.ProvinceCitySelect ProvinceCitySelect;
    private PopupWindow popupWindow;
    private List<String> list_province;
    private List<String> list_city;
    private List<String> list_district;
    ProvinceData provinceData;
    private WheelView wv_city;
    private View view;
    private WheelView wv_province;
    private WheelView wv_district;

    public ProvinceCitySelect() {

    }

    public static com.techfansy.provinceCitySelect.ProvinceCitySelect createBottom(Activity context) {
        if (ProvinceCitySelect == null) {
            synchronized (context) {
                if (ProvinceCitySelect == null) {
                    ProvinceCitySelect = new ProvinceCitySelect(context);
                }
            }
        }
        return ProvinceCitySelect;
    }

    private ProvinceCitySelect(Activity context) {
        createDialog(context);
    }

    /**
     * 初始化弹窗数据
     *
     * @param context 当前上下文
     */
    private void createDialog(final Activity context) {
        view = LayoutInflater.from(context).inflate(new Field().getLayoutId(context, "dialog_bottom")
                , null);
        //创建数据
        createData(context);
        //把省的数据转换为List<STRING>集合
        TextView tvSure = view.findViewById(new Field().getViewId(context, "tvSure"));
        TextView tvCancel = view.findViewById(new Field().getViewId(context, "tvCancel"));

        list_province = StringToArrays(provinceData.mProvinceDatas);
        list_city = StringToArrays(provinceData.mCitisDatasMap.get(list_province.get(0)));
        list_district = StringToArrays(provinceData.mDistrictDatasMap.get(list_city.get(0)));
        //绑定数据WheelView并且监听数据
        wv_province = bindData("wV_selectProvince", view, list_province, context);
        wv_province.setXoffset(5);
        wv_city = bindData("wV_selectCity", view, list_city, context);
        wv_city.setXoffset(-5);
        wv_district = bindData("wV_selectDetails", view, list_district, context);
        wv_district.setXoffset(-5);
        wv_province.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (index >= 0 && index <= list_province.size()) {
                    list_city = StringToArrays(provinceData.mCitisDatasMap.get(list_province.get(index)));
                    wv_city.setAdapter(new NumericWheelAdapter(list_city));
                    list_district = StringToArrays(provinceData.mDistrictDatasMap.get(list_city.get(0)));
                    wv_district.setAdapter(new NumericWheelAdapter(list_district));
                }
            }
        });
        wv_city.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (index >= 0 && index <= list_city.size()) {
                    list_district = StringToArrays(provinceData.mDistrictDatasMap.get(list_city.get(index)));
                    wv_district.setAdapter(new NumericWheelAdapter(list_district));
                }
            }
        });
        wv_province.setAdapter(new NumericWheelAdapter(list_province));


        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null)
                    onClickListener.sure(wv_province.getCurrentItemStr(), wv_city.getCurrentItemStr(), wv_district.getCurrentItemStr());
                dismiss();

            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        popupWindow = new PopupWindow(context, view, true, true);
    }

    public com.techfansy.provinceCitySelect.ProvinceCitySelect setProvinceCurrent(int CurrentItem) {
        wv_province.setCurrentItem(CurrentItem);
        return this;
    }

    public com.techfansy.provinceCitySelect.ProvinceCitySelect setCityCurrent(int CurrentItem) {
        wv_city.setCurrentItem(CurrentItem);
        return this;
    }

    /**
     * 把字符串数组转换为list<String>集合数据
     *
     * @param strings
     * @return
     */
    private List<String> StringToArrays(String[] strings) {
        return Arrays.asList(strings);
    }

    /**
     * 初始化获取asstet中并加载数据
     *
     * @createData()
     */
    private void createData(Activity context) {
        provinceData = new ProvinceData();
        provinceData.initProvinceDatas(context);
    }

    private OnClickListener onClickListener;

    public com.techfansy.provinceCitySelect.ProvinceCitySelect setSureListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    public interface OnClickListener {
        void sure(String strProvince, String strCity, String strDistrict);
    }

    /**
     * bingData
     *
     * @param R    控件id
     * @param view 布局view
     * @return WheelView控件
     */
    private WheelView bindData(String R, View view, List<String> list, Activity context) {
        WheelView wv_province = view.findViewById(ResourceHelper.getInstance(context).getId(R));
        wv_province.setTextSize(15);
        wv_province.setAdapter(new NumericWheelAdapter(list));
        return wv_province;
    }

    /**
     * 展示弹窗
     *
     * @show()
     */
    public com.techfansy.provinceCitySelect.ProvinceCitySelect show() {
        if (popupWindow != null)
            popupWindow.show();
        return this;
    }

    /**
     * 隐藏弹窗
     *
     * @show()
     */

    public void dismiss() {
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow.cancel();
            ProvinceCitySelect = null;
        }

    }


}
